//
//  String+Exs.swift
//  ttjx
//
//  Created by 文 on 2019/11/18.
//  Copyright © 2019 ttjx. All rights reserved.
//

import Foundation
import CommonCrypto

extension String {
    
    func sha256() -> String{
        if let stringData = self.data(using: String.Encoding.utf8) {
            return hexStringFromData(input: digest(input: stringData as NSData))
            
        }
        return ""
        
    }
    
    func isLetterWithDigital() ->Bool{
//        let numberRegex:NSPredicate=NSPredicate(format:"SELF MATCHES %@","^.*[0-9]+.*$")
//        let letterRegex:NSPredicate=NSPredicate(format:"SELF MATCHES %@","^.*[A-Za-z]+.*$")
//        if numberRegex.evaluate(with: self) && letterRegex.evaluate(with: self){
        let regex:NSPredicate=NSPredicate(format:"SELF MATCHES %@","^[a-zA-Z0-9]+$")
        if regex.evaluate(with: self){
           return true
        }else{
            return false
        }
    }
    
    func judgeIncludeChineseWord() -> Bool {
        for (_, value) in self.enumerated() {

            if ("\u{4E00}" <= value  && value <= "\u{9FA5}") {
                return true
            }
        }
        
        return false
    }
    
    private func digest(input : NSData) -> NSData {
        let digestLength = Int(CC_SHA256_DIGEST_LENGTH)
        var hash = [UInt8](repeating: 0, count: digestLength)
        CC_SHA256(input.bytes, UInt32(input.length), &hash)
        return NSData(bytes: hash, length: digestLength)
        
    }
    private  func hexStringFromData(input: NSData) -> String {
        var bytes = [UInt8](repeating: 0, count: input.length)
        input.getBytes(&bytes, length: input.length)
        var hexString = ""
        for byte in bytes {
            hexString += String(format:"%02x", UInt8(byte))
            
        }
        return hexString
        
    }
    
    var htmlToAttributedString: NSAttributedString? {
            guard let data = data(using: .utf8) else { return NSAttributedString() }
            do {
                return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
            } catch {
                return NSAttributedString()
            }
        }
    func htmlAttributedString() -> NSAttributedString? {
            guard let data = self.data(using: .utf8) else {
                return nil
            }

            return try? NSAttributedString(
                data: data,
                options: [.documentType: NSAttributedString.DocumentType.html],
                documentAttributes: nil
            )
        }
    var htmlToString: String {
        return htmlToAttributedString?.string ?? ""
    }
    var isBlank : Bool{
        return allSatisfy({$0.isWhitespace})
    }
}

extension Optional where Wrapped == String{
    var isBlank : Bool{
        return self?.isBlank ?? true
    }
}

//MARK: 类型转换
extension String {
    
    /**
     * 字符串 转 Int
     */
    public var intValue: Int {
        let str = self
        return Int(str) ?? 0
    }
    
    /**
     * 字符串 转 Float
     */
    public var floatValue: Float {
        let str = self
        return Float(str) ?? 0
    }
    
    /**
     * 字符串 转 Double
     */
    public var doubleValue: Double {
        let str = self
        return Double(str) ?? 0
    }
    
    /**
     * 字符串 转 Number
     */
    public var numberValue: NSNumber {
        let str = self
        let value = Int(str) ?? 0
        return NSNumber.init(value: value)
    }
    //MARK:- 字符串转时间戳
   func timeStrChangeTotimeInterval(_ dateFormat:String? = "yyyy-MM-dd") -> String {
       if self.isEmpty {
           return ""
       }
       let format = DateFormatter.init()
       format.dateStyle = .medium
       format.timeStyle = .short
       if dateFormat == nil {
           format.dateFormat = "yyyy-MM-dd"
       }else{
           format.dateFormat = dateFormat
       }
       let date = format.date(from: self)
       return String(date!.timeIntervalSince1970)
   }
}



extension String {
    
    /**
     * 判断是否电话号码 11位并且首位是1
     */
    public func mc_isPhoneNumber() -> Bool {
        if self.count != 11 { return false }
        if self.first != "1" { return false }
        return true
    }

    
    /**
     * 校验密码强度
     * 必须包含字母和数字，长度必须大于等于6
     */
    public func mc_isPassword() -> Bool {
        
        if self.count <= 5 {
            return false
        }
        
        let numberRegex:NSPredicate = NSPredicate(format:"SELF MATCHES %@","^.*[0-9]+.*$")
        let letterRegex:NSPredicate = NSPredicate(format:"SELF MATCHES %@","^.*[A-Za-z]+.*$")
        if numberRegex.evaluate(with: self) && letterRegex.evaluate(with: self) {
            return true
        } else {
            return false
        }
    }
    
}




extension String {
    
    /**
     * 计算字符串的高度
     */
    public func mc_getHeight(font: CGFloat, width: CGFloat) -> CGFloat {
        return mc_getHeight(font: UIFont.systemFont(ofSize: font), width: width)
    }
    
    public func mc_getHeight(font: UIFont, width: CGFloat) -> CGFloat {
        let statusLabelText: NSString = self as NSString
        let size = CGSize.init(width: width, height: 9000)
        let dic = NSDictionary(object: font, forKey: NSAttributedString.Key.font as NSCopying)
        let strSize = statusLabelText.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: dic as? [NSAttributedString.Key : Any], context: nil).size
        return strSize.height
    }
    
    /**
     * 计算字符串的宽度
     */
    public func mc_getWidth(font: CGFloat, height: CGFloat) -> CGFloat {
        return mc_getWidth(font: UIFont.systemFont(ofSize: font), height: height)
    }
    public func mc_getWidth(font:UIFont,height:CGFloat) -> CGFloat {
        let statusLabelText: NSString = self as NSString
        let size = CGSize.init(width: 9999, height: height)
        let dic = NSDictionary(object: font, forKey: NSAttributedString.Key.font as NSCopying)
        let strSize = statusLabelText.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: dic as? [NSAttributedString.Key : Any], context: nil).size
        return strSize.width
    }
    
    
    /**
     * 截取指定的区间
     */
    public func mc_clip(range: (location: Int, length: Int)) -> String {
        
        if range.location + range.length >= self.count {
            return self
        }
        let locationIndex = self.index(startIndex, offsetBy: range.location)
        let range = locationIndex..<self.index(locationIndex, offsetBy: range.length)
        return String(self[range])
    }
    
    
    /**
     * 字符串的截取 从头截取到指定index
     */
    public func mc_clipFromPrefix(to index: Int) -> String {
        
        if self.count <= index {
            return self
        } else {
            let index = self.index(self.startIndex, offsetBy: index)
            let str = self.prefix(upTo: index)
            return String(str)
        }
    }
    /**
     * 字符串的截取 从指定位置截取到尾部
     */
    public func mc_cutToSuffix(from index: Int) -> String {
        if self.count <= index {
            return self
        } else {
            let selfIndex = self.index(self.startIndex, offsetBy: index)
            let str = self.suffix(from: selfIndex)
            return String(str)
        }
    }
}



extension String {
    
    /**
     * 设置文本的颜色
     */
    public func mc_setMutableColor(_ color: UIColor,range: NSRange) -> NSAttributedString {
        let attributedString = NSMutableAttributedString.init(string: self)
        attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
        return attributedString
    }
    /**
     * 设置文本的字体大小
     */
    public func mc_setMutableFont(_ font: CGFloat, range: NSRange) -> NSAttributedString {
        let attributedString = NSMutableAttributedString.init(string: self)
        attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: font), range: range)
        return attributedString
    }
    
    /**
     * 设置文本的字体大小和颜色
     */
    public func mc_setMutableFontAndColor(font: CGFloat,
                                          fontRange: NSRange,
                                          color: UIColor,
                                          colorRange: NSRange)
        -> NSAttributedString {
            let attributedString = NSMutableAttributedString.init(string: self)
            attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: colorRange)
            
            attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: font), range: fontRange)
            return attributedString
    }
    
    /**
     * 设置删除线 NSStrikethroughStyleAttributeName
     */
    public func mc_setDeleteLine() -> NSAttributedString {
        let range = NSMakeRange(0, self.count)
        let attributedString = NSMutableAttributedString.init(string: self)
        attributedString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 1, range: range)
        return attributedString
    }
    
    /**
     * 同时设置设置删除线和字体大小
     */
    public func mc_setMutableFontAndDeleteLine(_ font: CGFloat,range: NSRange) -> NSAttributedString {
        let attributedString = NSMutableAttributedString.init(string: self)
        attributedString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 1, range: range)
        
        let deleTeRange = NSMakeRange(0, self.count)
        attributedString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 1, range: deleTeRange)
        
        return attributedString
    }
    
    
    /**
     * 设置文本的行间距
     */
    public func mc_setLineSpace(lineSpace: CGFloat) -> NSAttributedString {
        let attributedString = NSMutableAttributedString.init(string: self)
        let paragraphStyle = NSMutableParagraphStyle.init()
        paragraphStyle.lineSpacing = lineSpace
        let range = NSRange.init(location: 0, length: self.count)
        attributedString.addAttributes([NSAttributedString.Key.paragraphStyle:paragraphStyle], range: range)
        
        return attributedString
    }
    
    
    
    
    /**
     * 设置图文详情
     */
    public func mc_setTextAttachment(image: UIImage, imageFrame: CGRect) -> NSMutableAttributedString {
        
        let attributedStrM : NSMutableAttributedString = NSMutableAttributedString()
        let nameStr : NSAttributedString = NSAttributedString(string: " " + self, attributes: nil)
        
        let attachment = NSTextAttachment.init()
        attachment.image = image
        attachment.bounds = imageFrame
        
        attributedStrM.append(NSAttributedString(attachment: attachment))
        attributedStrM.append(nameStr)
        return attributedStrM
    }
}

